﻿using AutoMapper;
using MediatR;
using SCM.API.Write.Application.Command.Check;
using SCM.Domain.StockCheck;
using SCM.ErrorCode;
using SCM.Infrastructure.Implement;

namespace SCM.API.Write.Application.Handler.Check
{
    public class AddProfitLossHandler : IRequestHandler<AddProfitLossCommand, APIResult<int>>
    {
        IMapper mapper;
        DatabaseContext db;

        public AddProfitLossHandler(IMapper mapper, DatabaseContext db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(AddProfitLossCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = mapper.Map<ProfitLossModel>(request);

            int r = db.SqlSugar.Insertable(model).ExecuteCommand();

            result.Msg = "添加成功";
            result.Data = r;
            result.Code = APIEnum.Success;
            return await Task.FromResult(result);
        }
    }
}
